package ants.service;

import java.io.IOException;
import java.net.URLEncoder;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

public class BingSearchService {	
	public Set<String> getSearchResult(String keyword) {
		Set<String>  searchResult = new HashSet<String>();
		try {
			String encodedKeyword = URLEncoder.encode(keyword,"UTF-8");
			Document doc = Jsoup.connect("http://cn.bing.com/search?q="+encodedKeyword).get();
			Elements elements = doc.getElementsByTag("a");
			for (Element element : elements) {
				if(element.hasAttr("href") && element.hasAttr("target")) {
					String linkData = element.attr("href");
					linkData = linkData.replaceAll("http://", "");
					linkData = linkData.replaceAll("/","^");
					int dash = linkData.indexOf("^");
					if(dash!=-1) {
						linkData = linkData.substring(0,dash).trim();
					}
					//System.out.println("url="+linkData);
					searchResult.add(linkData);
				}
				
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		return searchResult;
	}

	
	
}
